<?php
namespace app\daili\controller;
use app\dsadmin\model\PayModel;
use think\Db;

class Caiwu extends Base
{
    public function jilu()
    {
    
        $riqi = input('riqi');
        $map = [];

        $map['userid'] = session('dailiuid');
        $map['del']  = 0;

        if ($riqi)
        {
            $map['shijian'] = ['like', "%" . $riqi . "%"];
        }

        $pay = new PayModel();

        $Nowpage = input('get.page') ? input('get.page'):1;

        $limits = 20; // 获取总条数
        $count = $pay->getAllCount($map); //计算总页面
        $allpage = intval(ceil($count / $limits));

        $lists = $pay->getPayByWhere($map, $Nowpage, $limits);

        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数 
        $this->assign('riqi', $riqi);
        if (input('get.page'))
        {
            return json($lists);
        }
        $map = [];

        $dzfbs = Db::name("pay")->where(['zt' => "等待支付", 'userid' => session('dailiuid')])->count();
        $this->assign('dzfbs', $dzfbs);

        $money1 = Db::name("pay")->where(['zt' => "等待支付", 'userid' => session('dailiuid')])->sum('money');
        $this->assign('money1', empty($money1)?0:$money1);

        return $this->fetch();
    }

      public function sqtixian()
    {
        
        
      

        if (input("post."))
        {
            $data = input("post.");
            $money1 = Db::name("pay")->where(['userid' => session('dailiuid'), 'shijian' => ['like', date('Y-m-d')]])->sum('money');
            $member = Db::name("member")->where(['id' => session('dailiuid')])->find();
            if (floatval($data['money']) < config('txzdje'))
            {
                return ['code' => 0, 'msg' => '提现金额不能小于最小提现金额！'];
                die;
            }

            if (floatval($data['money']) + $money1 > config('mrtxzgje'))
            {
                return ['code' => 0, 'msg' => '超过每天可提款总金额！'];
                die;
            }

            if (floatval($data['money']) > config('aqtxje'))
            {
                return ['code' => 0, 'msg' => '超过提现单笔最高金额！'];
                die;
            }

            if (floatval($data['money']) > $member['money'])
            {
                return ['code' => 0, 'msg' => '提现金额不能大于余额！'];
                die;
            }
            
            
            if(!preg_match("/^\d+(\.\d+)?$/",$data['money'])){
            
                return ['code' => 0, 'msg' => '金额输入错误！'];
                die;
                
            }

            if (floatval(md5($data['password'])) != $member['password'])
            {
                return ['code' => 0, 'msg' => '登陆密码错误！'];
                die;
            }
            
            if (md5($data['txpassword']) == $member['password'])
            {
                return ['code' => 0, 'msg' => '提现密码不能和登录相同！'];
                die;
            }
            
            $resss = get_pwd_strength($data['txpassword']);
            
            if($resss !== true){
                return ['code' => 0, 'msg' => $resss];
               
            }
            
            if(empty($member['txpassword'])){
                Db::name('member')->where(['id' => $member['id']])->update(['txpassword' => md5($data['txpassword'])]);
            }
            
            if($member['txpassword'] != md5($data['txpassword'])  &&  !empty($member['txpassword'])){
                return ['code' => 0, 'msg' => '提现密码错误！'];
              
            }
            
            if(empty($data['txpassword'])){
                return ['code' => 0, 'msg' => '提现密码不能为空！'];
               
            }
            
            

            // if ($data['type'] == 1 && empty($data['imgurl']))
            // {
            //     return ['code' => 0, 'msg' => '请上传收款码！'];
            // }

            if ($data['type'] == 0 && (empty($data['name']) || empty($data['zhanghao']) || empty($data['leixing'])))
            {
                return ['code' => 0, 'msg' => '账户信息不完整！'];
            }
            
            //查询余额是否够
            $fp = fopen("lock.txt", "r");
            //加锁
            if(flock($fp,LOCK_EX | LOCK_NB))
            {
                $member = Db::name("member")->where(['id' => session('dailiuid')])->find();
               
                if(floatval($data['money']) <= $member['money']){
                    
                    $ress = Db::name("member")->where(['id' => session('dailiuid')])->setDec('money', floatval($data['money']));
                    $ress2 = Db::name("member")->where(['id' => session('dailiuid')])->setInc('txmoney', floatval($data['money']));
                    
                }
                
                $data['shijian'] = date("Y-m-d H:i:s");
                $data['userid'] = session('dailiuid');
                $data['zt'] = '等待支付';
                unset($data['password']);
                unset($data['file']);
                unset($data['txpassword']);
                Db::name("pay")->insert($data);
    
                return ['code' => 1, 'msg' => '申请成功！'];
                
                if($ress && $ress2){
                      //执行完成解锁
                     flock($fp,LOCK_UN);
                }
              
            }
                //关闭文件
            fclose($fp);
            
           

            
        }
        
         $money1 = Db::name("pay")->where(['userid' => session('dailiuid'), 'shijian' => ['like', date('Y-m-d')]])->sum('money');
         $member = Db::name("member")->where(['id' => session('dailiuid')])->find();

        $this->assign('money1', empty($money1)?0:$money1);

        $this->assign('member', $member);

        $zhsk = Db::name("pay")->where(['userid' => session('dailiuid')])->order('id desc')->find();
        $type = 2;
        if (!empty($zhsk))
        {
            $type = $zhsk['type'];
        }

        $zhsk = Db::name("pay")->where(['userid' => session('dailiuid'), 'type' => 1])->order('id desc')->find();

        $this->assign('zhsk', $zhsk);

        $zhsk2 = Db::name("pay")->where(['userid' => session('dailiuid'), 'type' => 0])->order('id desc')->find();

        $this->assign('zhsk2', $zhsk2);

        $this->assign('type', $type);

        return $this->fetch();
    }
}
